package com.lh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lh.domian.ResponseResults;
import com.lh.domian.entity.Article;
import com.lh.domian.entity.Category;
import com.lh.domian.vo.ArticleDetailVo;
import com.lh.domian.vo.ArticleListVo;
import com.lh.domian.vo.HotArticleVo;
import com.lh.domian.vo.PageVo;
import com.lh.mapper.ArticleMapper;
import com.lh.mapper.CategoryMapper;
import com.lh.service.ArticleService;
import com.lh.utils.BeanCopyUtils;
import com.lh.utils.RedisCache;
import com.lh.utils.SystemCodes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Author LuoHang
 * @Date 2022-08-25 21:45
 **/
@Service("articleService")
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public ResponseResults hotArticleList() {
        //获取热文章
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Article.class,tableFieldInfo -> "id".equals(tableFieldInfo.getColumn()) ||
                "title".equals(tableFieldInfo.getColumn()) ||
                "view_count".equals(tableFieldInfo.getColumn()));
        //以发布的文章
        queryWrapper.eq(Article::getStatus, SystemCodes.STATUS_NORMAL);
        //按照浏览量排序
        queryWrapper.orderByDesc(Article::getViewCount);
        //最多只查询10个
        Page<Article> page = new Page<>();
        page.setCurrent(1);
        page.setSize(10);
        articleMapper.selectPage(page, queryWrapper);
        List<Article> articles = page.getRecords();
        List<HotArticleVo> hotArticleVos = BeanCopyUtils.copyBeans(articles,HotArticleVo.class);
        return new ResponseResults(200,hotArticleVos);
    }

    @Override
    public ResponseResults articleList(Long categoryId, Integer pageNum, Integer pageSize) {
        //查询条件
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        //有分类的id的需要加id
        queryWrapper.eq(Objects.nonNull(categoryId) && categoryId > 0, Article::getCategoryId, categoryId);
        //状态是正式发布的
        queryWrapper.eq(Article::getStatus, SystemCodes.STATUS_NORMAL);
        //根据isTop 降序
        queryWrapper.orderByDesc(Article::getIsTop);
        //分页
        Page<Article> page = new Page<>(pageNum,pageSize);
        articleMapper.selectPage(page,queryWrapper);
        List<ArticleListVo> articleListVos = BeanCopyUtils.copyBeans(page.getRecords(), ArticleListVo.class);
        PageVo pageVo = new PageVo(articleListVos,page.getTotal(),pageNum,pageSize);
        return new ResponseResults(200,pageVo);
    }

    @Override
    public ResponseResults getArticleDetail(Long id) {
        Article article = getById(id);
        //获取redis中的实时viewCount数
        Long viewCount = redisCache.getCacheMapValue("article:viewCount",id.toString());
        article.setViewCount(viewCount);
        //转换VO
        ArticleDetailVo articleDetailVo = BeanCopyUtils.copyBean(article, ArticleDetailVo.class);
        Long categoryId = article.getCategoryId();
        Category category = categoryMapper.selectById(categoryId);
        articleDetailVo.setCategoryName(category.getName());
        return new ResponseResults(200,articleDetailVo);
    }

    @Override
    public ResponseResults updateViewCount(Long id) {
        redisCache.incrementCacheMapValue("article:viewCount",id.toString(),1);
        return new ResponseResults();
    }
}
